/*------------------------------------------------------------------------------------------------\
| Horrible X-Wing game by Steven Haber
| st_haber@hotmail.com
|
| enemy.h: contains class Ai, which generates enemies based on "modes"
\------------------------------------------------------------------------------------------------*/

#include "Ai.h"


//Sets all the defaults, note it is manually controlled to begin with
Ai::Ai(sf::Sprite craftSprite, sf::Sprite *layzarSprite, int offsetX, int offsetY, sf::RenderWindow *out, Sound *sounds, int scoreValue)
{
	mCurrentMode = SIMPLE;
	mNextMode = SIMPLE;
	mManual = true;
	mNumberEnemies = 0;
	mResetting = false;
	mScore = scoreValue;

	mCraftSprite = craftSprite;
	mCraftSprite.SetRotationCenter(mCraftSprite.GetWidth()/2, mCraftSprite.GetHeight()/2);
	mCraftSprite.SetRotation(180);
	mLayzarOffsetX = offsetX;
	mLayzarOffsetY = offsetY;
	
	mLayzarSprite = layzarSprite;
	mOut = out;
	mSounds = sounds;

	for(int i = 0; i < NUMBERENEMIES; i++)
		mEnemies[i] = NULL;

	mTimer.Reset();
	mTimer2.Reset();
}


//Call this anytime you want the AI to do something
void Ai::go(float delta)
{	
	if(mNumberEnemies == 0)
	{
		if(!mManual)
			mCurrentMode = sf::Randomizer::Random(0, NUMBERMODES - 1);
		else if(mCurrentMode != mNextMode)
			mCurrentMode = mNextMode;
	}
		

	if(mCurrentMode == SIMPLE)
	{

		//Spawns the enemies if the mode hasn't finisehd spawning yet
		if(mNumberEnemies < 3 && mTimer.GetElapsedTime() > 1)
		{
			mTimer.Reset();

			//Finds a clear spot, creates a new enemy
			int i = 0;
			while(i < 3)
			{
				if(mEnemies[i] == NULL)
				{
					mEnemies[i] = new Enemy(&mCraftSprite, mLayzarOffsetX, mLayzarOffsetY, mOut, mLayzarSprite, sf::Randomizer::Random((float)0, (float)SCREENX - mCraftSprite.GetWidth()), LAYZARDAMAGE * 4, mSounds);
					mNumberEnemies++;
					mEnemyTimer[i].Reset();
					i = 3;
				}

				i++;
			}
		}

		//Checks for dead guys
		if(!mResetting)
		{
			int counter = 0;
			for(int i = 0; i < 3; i++)
			{
				if(mEnemies[i] != NULL && mEnemies[i]->mDead)
					counter++;
			}

			if(counter == 3)
			{
				mTimer.Reset();
				mResetting = true;
			}
		}

		else if(mTimer.GetElapsedTime() > 2)
		{
			mResetting = false;
			mNumberEnemies = 0;

			for(int j = 0; j < 3; j++)
			{
				delete *(mEnemies+j);
				mEnemies[j] = NULL;
			}
		}
			
		//Tells all of the spawned guys what to do
		for(int i = 0; i < 3; i++)
		{
			if(mEnemies[i] != NULL)
			{
				if(mEnemyTimer[i].GetElapsedTime() < .3)
					mEnemies[i]->input(delta, false, true, false, false, false);
				else
				{
					int temp = (int)sf::Randomizer::Random((float)1, (float)1/delta);
					bool temp2 = false;
					if(temp == 1)
					temp2 = true;
				
					if(mTimer2.GetElapsedTime() < 2)
						mEnemies[i]->input(delta, false, false, false, true, temp2);
					else if(mTimer2.GetElapsedTime() < 4)
						mEnemies[i]->input(delta, false, false, true, false, temp2);
					else
					{
						mTimer2.Reset();
						mEnemies[i]->input(delta, false, false, true, false, temp2);
					}
				}
			}
		}
	}

	if(mCurrentMode == SIMPLE_TWO)
	{

		//Spawns the enemies if the mode hasn't finisehd spawning yet
		if(mNumberEnemies < 6 && mTimer.GetElapsedTime() > 1)
		{
			mTimer.Reset();

			//Finds a clear spot, creates a new enemy
			int i = 0;
			while(i < 6)
			{
				if(mEnemies[i] == NULL)
				{
					mEnemies[i] = new Enemy(&mCraftSprite, mLayzarOffsetX, mLayzarOffsetY, mOut, mLayzarSprite, sf::Randomizer::Random((float)0, (float)SCREENX - mCraftSprite.GetWidth()), LAYZARDAMAGE * 4, mSounds);
					mNumberEnemies++;
					mEnemyTimer[i].Reset();
					i = 6;
				}

				i++;
			}
		}

		//Checks for dead guys
		if(!mResetting)
		{
			int counter = 0;
			for(int i = 0; i < 6; i++)
			{
				if(mEnemies[i] != NULL && mEnemies[i]->mDead)
					counter++;
			}

			if(counter == 6)
			{
				mTimer.Reset();
				mResetting = true;
			}
		}

		else if(mTimer.GetElapsedTime() > 2)
		{
			mResetting = false;
			mNumberEnemies = 0;

			for(int j = 0; j < 6; j++)
			{
				delete *(mEnemies+j);
				mEnemies[j] = NULL;
			}
		}
			
		//Tells all of the spawned guys what to do
		for(int i = 0; i < 6; i++)
		{
			if(mEnemies[i] != NULL)
			{
				if((i < 3 && mEnemyTimer[i].GetElapsedTime() < .3) || (i >=3  && mEnemyTimer[i].GetElapsedTime() < .6))
					mEnemies[i]->input(delta, false, true, false, false, false);
				else
				{
					int temp = (int)sf::Randomizer::Random((float)1, (float)1/delta);
					bool temp2 = false;
					if(temp == 1)
					temp2 = true;
				
					if(mTimer2.GetElapsedTime() < 2)
						mEnemies[i]->input(delta, false, false, false, true, temp2);
					else if(mTimer2.GetElapsedTime() < 4)
						mEnemies[i]->input(delta, false, false, true, false, temp2);
					else
					{
						mTimer2.Reset();
						mEnemies[i]->input(delta, false, false, true, false, temp2);
					}
				}
			}
		}
	}

	if(mCurrentMode == SIMPLE_THREE)
	{

		//Spawns the enemies if the mode hasn't finisehd spawning yet
		if(mNumberEnemies < 9 && mTimer.GetElapsedTime() > 1)
		{
			mTimer.Reset();

			//Finds a clear spot, creates a new enemy
			int i = 0;
			while(i < 9)
			{
				if(mEnemies[i] == NULL)
				{
					mEnemies[i] = new Enemy(&mCraftSprite, mLayzarOffsetX, mLayzarOffsetY, mOut, mLayzarSprite, sf::Randomizer::Random((float)0, (float)SCREENX - mCraftSprite.GetWidth()), LAYZARDAMAGE * 4, mSounds);
					mNumberEnemies++;
					mEnemyTimer[i].Reset();
					i = 9;
				}

				i++;
			}
		}

		//Checks for dead guys
		if(!mResetting)
		{
			int counter = 0;
			for(int i = 0; i < 9; i++)
			{
				if(mEnemies[i] != NULL && mEnemies[i]->mDead)
					counter++;
			}

			if(counter == 9)
			{
				mTimer.Reset();
				mResetting = true;
			}
		}

		else if(mTimer.GetElapsedTime() > 2)
		{
			mResetting = false;
			mNumberEnemies = 0;

			for(int j = 0; j < 9; j++)
			{
				delete *(mEnemies+j);
				mEnemies[j] = NULL;
			}
		}
			
		//Tells all of the spawned guys what to do
		for(int i = 0; i < 9; i++)
		{
			if(mEnemies[i] != NULL)
			{
				if((i < 3 && mEnemyTimer[i].GetElapsedTime() < .3) || ((i >=3  && i < 6) && mEnemyTimer[i].GetElapsedTime() < .6) || (i >= 6 && mEnemyTimer[i].GetElapsedTime() < .9))
					mEnemies[i]->input(delta, false, true, false, false, false);
				else
				{
					int temp = (int)sf::Randomizer::Random((float)1, (float)1/delta);
					bool temp2 = false;
					if(temp == 1)
					temp2 = true;
				
					if(mTimer2.GetElapsedTime() < 2)
						mEnemies[i]->input(delta, false, false, false, true, temp2);
					else if(mTimer2.GetElapsedTime() < 4)
						mEnemies[i]->input(delta, false, false, true, false, temp2);
					else
					{
						mTimer2.Reset();
						mEnemies[i]->input(delta, false, false, true, false, temp2);
					}
				}
			}
		}
	}
}


//Sets if mode changing is manual or automatic, bool
void Ai::manualModeSet(bool yes)
{
	mManual = yes;
}


//Set the mode from argument
void Ai::setMode(int mode)
{
	mNextMode = mode;
}


//Returns the number of enemies currently being processed by Ai
int Ai::getNumberEnemies()
{
	return mNumberEnemies;
}


//Collision check for enemy layzars with passed in craft
int Ai::collisionCheck(int enemyNumber, Craft *craft)
{
	return mEnemies[enemyNumber]->collisionCheck(craft);
}


//Draws all layzars for all enemy craft present
void Ai::drawLayzars(float delta)
{
	for(int i = 0; i < NUMBERENEMIES; i++)
	{
		if(mEnemies[i] != NULL)
			mEnemies[i]->drawLayzars(delta);
	}
}


//Draws all of the enemies currently defined
void Ai::drawEnemies()
{
	for(int i = 0; i < NUMBERENEMIES; i++)
	{
		if(mEnemies[i] != NULL)	
			mEnemies[i]->draw();
	}
}


//Returns an enemy craft for use with collision detection
Craft *Ai::getCraft(int enemyNumber)
{
	return mEnemies[enemyNumber]->getCraft();
}

//Hurter
bool Ai::hurt(int enemyNumber, int amount)
{
	return mEnemies[enemyNumber]->hurt(amount);
}

//Killer
void Ai::kill(int enemyNumber)
{
	mEnemies[enemyNumber]->kill();
}


//Returns the score value for the enemies of the ai
int Ai::getScore()
{
	return mScore;
}


//For setting manual or random mode selection
void Ai::setManual(bool manual)
{
	mManual = manual;
}